package com.liao.healthydiet;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.liao.healthydiet.domain.Food;

public class GridViewFoodNLayoutAdapter extends BaseAdapter {

    private List<Food> foods;

    private Context context;
    private LayoutInflater layoutInflater;

    public GridViewFoodNLayoutAdapter(Context context, ArrayList<Food> foods) {
        this.context = context;
        this.foods = foods;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return foods.size();
    }

    @Override
    public Food getItem(int position) {
        return foods.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.gridview_footn_layout, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((Food) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(Food food, ViewHolder holder) {
        holder.imgFoodImg.setImageResource(food.getImage());
        holder.tvFoodName.setText(food.getName());
    }

    protected class ViewHolder {
        private ImageView imgFoodImg;
        private TextView tvFoodName;

        public ViewHolder(View view) {
            imgFoodImg = (ImageView) view.findViewById(R.id.img_food_img);
            tvFoodName = (TextView) view.findViewById(R.id.tv_food_name);
        }
    }
}
